<%@ page pageEncoding="UTF-8"%>
<%@ include file="/common/token.jsp"%>
<%@ include file="/common/user.jsp"%>
<%@ include file="/common/freqmap.jsp"%>
<%
    Integer pid = input.getInt("pid", 0);
    Integer uid = token.getUid();
    Integer branch = input.getInt("branch", 1);
    Map<Integer, UserProfile> userprofileMap = userprofileByUid(uid);
    Map<String, Integer> cardMap = new HashMap<String, Integer>();
    String hql = "";
    if (pid == 0) { // 查全部就诊人
        String cards = "'320502199309193011'";
        for (Integer key : userprofileMap.keySet()) {
            UserProfile up = userprofileMap.get(key);
            String cardNumber = up.getCardNumber();
            cardMap.put(cardNumber, key);
            cards += ",'" + cardNumber + "'";
        }
        hql = "select distinct new map(patient_name as patient_name, branch_name as branch_name, sample_no as sample_no, name as name, price as price, amount as amount, unit as unit, usage as usage, content as content, time as time, card_number as card_number, way as way) from medicine where card_number in (" + cards + ") and hospital=" + hospital +" and branch = " + branch + " order by time desc";
    } else {
        UserProfile userprofile = userprofileMap.get(pid);
        if (userprofile == null) {
            msg.setErrcodeAndErrmsg(30021, "pid不属于该用户");
            Tools.encryptPrint(out, msg);
            return;
        }
        String cardNumber = userprofile.getCardNumber();
        hql = "select distinct new map(patient_name as patient_name, branch_name as branch_name, sample_no as sample_no, name as name, price as price, amount as amount, unit as unit, usage as usage, content as content, time as time, card_number as card_number, way as way) from medicine where card_number = '" + cardNumber + "' and hospital=" + hospital +" and branch = " + branch + " order by time desc";
    }
    List medicineList = QBuilder.db("basedb").queryHql(hql);
    if (isEmpty(medicineList)) {
        msg = new Message(30041, "暂无用药记录");
    }
    List result = new ArrayList();
    Map<String, List> temp = new LinkedHashMap<String, List>();
    for (int i = 0; i < medicineList.size(); i++) {
        Map ma = (Map)medicineList.get(i);
		ma.put("usage", freqMap.get(Tools.getDesc(ma.get("usage"), ma.get("usage") + "")));
        String cardNumber = Tools.getDesc(ma.get("card_number"), "");
        ma.put("pid", cardMap.get(cardNumber));
        ma.remove("card_number");
        String key = Tools.getDesc(ma.remove("pid"), i+"") + "_" + Tools.getDesc(ma.remove("time"), i+"")+ "_" + Tools.getDesc(ma.remove("sample_no"), "");
        List tempList = (List)temp.get(key);
        if(tempList == null) {
            tempList = new ArrayList();
            temp.put(key, tempList);
        }
        tempList.add(ma);
    }
    Set<Entry<String, List>> set = temp.entrySet();
    List<Map> _result = new ArrayList<Map>();
    for (Entry<String, List> entry : set) {
        String key = entry.getKey();
        List values = entry.getValue();
        Map map = new HashMap();
        String[] strs = key.split("_");
        map.put("pid", strs[0]);
        map.put("time", strs[1]);
        map.put("sample_no", strs[2]);
        map.put("result", values);
        _result.add(map);
    }
    msg.put("data", _result);
    Tools.encryptPrint(out, msg);
%>
